package com.ruoyi.bill.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author SunFanKe
 * @CreateTime 2025-02-24
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        // 获取当前登录用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //设置创建时间
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        //设置添加人
        this.strictInsertFill(metaObject, "createBy", String.class, loginUser.getUsername());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 获取当前登录用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //设置修改时间
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        //设置修改人
        this.strictInsertFill(metaObject, "updateBy", String.class, loginUser.getUsername());
    }
}
